package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 定时器,使得redis数据持久化
 */
@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    //表示该方法是定时任务方法
    @Scheduled(cron = "0/5 * * * * ?") //cron 表示任务计划  定时策略
    public void dosomething() {
        System.out.println("---------start------------" + System.currentTimeMillis());

        //获取需要持久化的数据
        List<StrategyStatisVO> vos = strategyStatisRedisService.getStrategyStatisVOS(RedisKeys.STRATEGY_STATIS_VO.getPrefix());

        for (StrategyStatisVO vo : vos){
            StrategyStatisPersistenceVO pvo = new StrategyStatisPersistenceVO();
            try {
                BeanUtils.copyProperties(pvo,vo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            strategyDetailService.updateStatisVO(pvo);
        }
    }
}
